package kr.or.javacafe.magic;

public class SixMagicSquare extends AbstractMagic{
	public SixMagicSquare(){
		this(6);
	}
	
	public SixMagicSquare(int n){
		this.init(n);
	}
	
	public void init(int n){
		super.magic = new int[n][n];
		super.top = n-1;
	}
	
	public void make(){
		makeA();
		makeB();
		makeCD();
		multiples();
		addABCD();
	}
	
	public void addABCD() {
		int count = top+1;
		
		OddMagicSquare odd = new OddMagicSquare(count/2);
		odd.make();
		int[][] oddM = odd.getMagic();
		
		for (int i = 0; i < count/2; i++) {
			for (int j = 0; j < count/2; j++) {
				magic[i][j] += oddM[i][j];
				magic[i][j+count/2] += oddM[i][j];
				magic[i+count/2][j] += oddM[i][j];
				magic[i+count/2][j+count/2] += oddM[i][j];				
			}
		}		
	}

	public void multiples() {
		int count = top +1;
		for (int i = 0; i < count; i++) {
			for (int j = 0; j < count; j++) {
				magic[i][j] *= count/2 * count/2;
			}
		}
	}

	public void makeCD() {
		int count = top+1;
		for (int i = 0; i < count/2; i++) {
			for (int j = 0; j < count/2; j++) {
				if (magic[i][j]==0) {
					magic[i+count/2][j]=3;
				}else if(magic[i][j]==3){
					magic[i+count/2][j]=0;
				}			
				if(magic[i][j+count/2]==2){
					magic[i+count/2][j+count/2]=1;
				}else if (magic[i][j+count/2]==1) {
					magic[i+count/2][j+count/2]=2;
				}		
			}
		}
	}

	public void makeB() {
		int count = top+1;
		for (int i = 0; i < count/2; i++) {
			for (int j = 0; j < count/2; j++) {
				magic[i][j+count/2]=1;
			}
		}
		
		for (int i = 0; i < count/2; i++) {
			for (int j = 0; j < count/2-(count/4-1); j++) {
				magic[i][j+count/2]=2;
			}
		}
	}

	public void makeA() {
		int count = top+1;
		for (int i = 0; i < count/2; i++) {
			for (int j = 0; j < count/4; j++) {
				if (i==count/4) {
					magic[i][j+1]=3;
				}else{
					magic[i][j]=3;
				}
			}
		}
	}
}













